<%@ page pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/base/taglibs.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<link href="${ctx}/css/main.css" rel="stylesheet" type="text/css" />
	</head>
	<script language="javascript">
		var message = '${message}';
		if(message != ''){
			alert(message);
		}
	
		function submitAction(action){
			document.accountForm.action = action ;
			document.accountForm.submit();
		}
		
		function init(){
			document.getElementById("accountName").value = "";
			document.getElementById("email").value = "";
			document.getElementById("password").value = "";
			document.getElementById("password1").value = "";
			document.getElementById("telNo").value = "";
			document.getElementById("roleId").value = "";
		}
		
		function adminAccountSave() {
			accountName = document.getElementById("accountName").value;
			accountId = document.getElementById("id").value;
			email = document.getElementById("email").value;
			password = document.getElementById("password").value;
			password1 = document.getElementById("password1").value;
			if(accountId != ''){
				oldpassword = document.getElementById("oldpassword").value;
			}
			if(accountName == ''){
				alert('请输入帐号名称');
				return;
			}
			if(!validateUsername()) return;
			if(email == ''){
				alert('请输入电子邮箱');
				return;
			}
			if(!validateEmail()) return;
			if(accountId != '' && (password != '' || password1 != '') && oldpassword == ''){
				alert('请输入旧密码');
				return;
			}
			if((accountId == '' && password == '') || (password == '' && password1 != '')){
				alert('请输入密码');
				return;
			}
			if((accountId == '' && password1 == '') || (password != '' && password1 == '')){
				alert('请输入确认密码');
				return;
			}
			if((password != '' || password1 != '') && password != password1) {
				alert('两次密码必须一致');
				document.getElementById("password1").value = "";
				return;
			}
			document.accountForm.action = 'adminAccountSave.ac' ;
			document.accountForm.submit();
		}
		
		function validateEmail() {
			var emailpattern = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/; 
		   	email = document.getElementById("email").value; 
			if(emailpattern.test(email) == false) {
				alert('电子邮箱栏位不是有效的电子邮箱地址格式，请重新输入');
				return false;
			}
			return true;
		}
		
		function validatePassword() {
			password = document.getElementById("password").value;
			password1 = document.getElementById("password1").value;
			if(password != password1) {
				alert('两次密码必须一致');
				document.getElementById("password1").value = "";
				return;
			}
		}
		
		function validateUsername() {
			var pattern = /^[a-zA-Z]{1}[0-9a-zA-Z_]{1,}$/;
			accountName = document.getElementById("accountName").value; 
			if(accountName.length < 4) {
				alert('帐户名称不能少于4个字符')
				return false;
			}
			if(pattern.test(accountName) == false) {
				alert('帐户名称只能以字母开头，且必须由字母、数字或下划线组成');
				return false;
			}
			return true;
		}
	</script>
	
	<body>
		<form method="post" action="" name="accountForm">
			<table border="0" cellspacing="1" cellpadding="3" align="center" width="100%">
				<tr>
					<td class="td11" align="left" colspan="4">
						<input type="button" name="search" value="保存" onClick="adminAccountSave()">
						<c:if test="${empty adminAccount.id}">&nbsp;&nbsp;<input type="button" name="reset" value="重置" onClick="init()"></c:if>
						<c:if test="${flag != 1}">&nbsp;&nbsp;<input type="button" name="new" value="返回" onClick="submitAction('adminAccountBack.ac')"></c:if>
					</td>
				</tr>
				<c:if test="${!empty adminAccount.id}">
					<tr>
						<td colspan="4"><span style="color:red">提示：如不需要更改密码，则密码栏位不用输入</span></td>
					</tr>
				</c:if>
				<tr>
					<td width="20%" class="td2"> 
						帐号名称 <span style="color:red;vertical-align:middle;">*</span>
					</td>
					<td width="30%" class="td2">
						<input type="hidden" name="id" value="${adminAccount.id}" />
						<input type="hidden" name="flag" value="${flag}" />
						<c:if test="${empty adminAccount.id}">
							<input type="text" name="accountName" size="32" value="${adminAccount.accountName}" onblur="validateUsername();">
						</c:if>
						<c:if test="${!empty adminAccount.id}">
							<input readonly="readonly" style="color:grey" type="text" name="accountName" size="32" value="${adminAccount.accountName}">
						</c:if>
					</td>
					<td width="20%" class="td2">
						电子邮箱 <span style="color:red;vertical-align:middle;">*</span>
					</td>
					<td width="30%" class="td2">
						<input type="text" name="email" size="32" value="${adminAccount.email}" onblur="validateEmail();">
					</td>
				</tr>
				<c:if test="${!empty adminAccount.id}">
					<tr>
						<td width="20%" class="td2"> 
							旧密码 <span style="color:red;vertical-align:middle;">*</span>
						</td>
						<td width="30%" class="td2">
							<input type="password" name="oldpassword" size="33" value="">
						</td>
						<td width="20%" class="td2">
							新密码 <span style="color:red;vertical-align:middle;">*</span>
						</td>
						<td width="30%" class="td2">
							<input type="password" name="password" size="33" value="">
						</td>
					</tr>
					<tr>
						<td width="20%" class="td2"> 
							确认密码 <span style="color:red;vertical-align:middle;">*</span>
						</td>
						<td width="30%" class="td2">
							<input type="password" name="password1" size="33" value="" onblur="validatePassword();">
						</td>
						<c:if test="${flag == 1}">
							<td width="20%" class="td2"> 
								电话号码 
							</td>
							<td width="30%" class="td2">
								<input type="text" name="telNo" size="32" value="${adminAccount.telNo}">
							</td>
						</c:if>
						<c:if test="${flag != 1}">
							<td width="20%" class="td2">
							</td>
							<td width="30%" class="td2">
							</td>
						</c:if>
					</tr>
				</c:if>
				<c:if test="${empty adminAccount.id}">
					<tr>
						<td width="20%" class="td2"> 
							密码 <span style="color:red;vertical-align:middle;">*</span>
						</td>
						<td width="30%" class="td2">
							<input type="password" name="password" size="33" value="">
						</td>
						<td width="20%" class="td2">
							确认密码 <span style="color:red;vertical-align:middle;">*</span>
						</td>
						<td width="30%" class="td2">
							<input type="password" name="password1" size="33" value="" onblur="validatePassword();">
						</td>
					</tr>
				</c:if>
				<tr>
					<c:if test="${flag != 1}">
						<td width="20%" class="td2"> 
							电话号码 
						</td>
						<td width="30%" class="td2">
							<input type="text" name="telNo" size="32" value="${adminAccount.telNo}">
						</td>
						<td width="20%" class="td2">
							角色
						</td>
						<td width="30%" class="td2">
							<select name="roleId" style="width:200px">
								<option value="" />
								<c:forEach var="role" items="${sessionScope.T_roleList}">
									<c:if test="${role.id == adminAccount.roleId}">
										<option value="${role.id}" selected="true" >${role.roleName}</option>
									</c:if>
									<c:if test="${role.id != adminAccount.roleId}">
										<option value="${role.id}" >${role.roleName}</option>
									</c:if>
								</c:forEach>
							</select>
						</td>
					</c:if>
				</tr>
			</table>
			<BR>
		</form>
	</body>
</html>